<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>URL Node.js v0.10.35 Manual &amp; Documentation</title>
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="http://nodejs.org/api/url.html">
  <script type="text/javascript" src="//use.typekit.net/mse5tqx.js"></script>
  <script type="text/javascript">try{Typekit.load();}catch(e){}</script>
</head>
<body class="alt apidoc int docs" id="docs">
    <div id="nav">
      <img id="logo" src="assets/logo.svg" alt="node.js">
      <ul>
        <li><a href="http://nodejs.org">Home</a></li>
        <li><a href="http://nodejs.org/download/">Downloads</a></li>
        <li class="active"><a href="http://nodejs.org/documentation/">Docs</a></li>
        <li><a href="http://nodejs.org/community/">Community</a></li>
        <li><a href="http://nodejs.org/about/">About</a></li>
        <li><a href="http://jobs.nodejs.org">Jobs</a></li>
        <li><a href="http://blog.nodejs.org">Blog</a></li>
      </ul>
    </div>
    <div id="content-wrap">
    <div id="content" class="clearfix">
      <div id="column2" class="interior">
        <!--<img src="/images/logo-light.svg" alt="node.js" width="170">-->
        <ul class="docs-nav">
          <li><a href="http://nodejs.org/documentation/">About Docs</a></li>
          <li><a href="http://nodejs.org/documentation/tutorials/">Tutorials</a></li>
          <li><a href="http://nodejs.org/documentation/contributing/">Contributing</a></li>
          <li><a href="http://nodejs.org/documentation/localization/">Localization</a></li>
          <li class="active"><a href="http://nodejs.org/api/">API Docs</a></li>
        </ul>
      </div>
        <div id="column1" class="interior">
          <header>
            <h1>Node.js v0.10.35 Manual &amp; Documentation</h1>
            <div id="gtoc">
              <p>
                <a href="index.html" name="toc">Index</a> |
                <a href="all.html">View on single page</a> |
                <a href="url.json">View as JSON</a>
              </p>
            </div>
            <hr>
          </header>

          <div id="toc">
            <h2>Table of Contents</h2>
            <ul>
<li><a href="#url_url">URL</a><ul>
<li><a href="#url_url_parse_urlstr_parsequerystring_slashesdenotehost">url.parse(urlStr, [parseQueryString], [slashesDenoteHost])</a></li>
<li><a href="#url_url_format_urlobj">url.format(urlObj)</a></li>
<li><a href="#url_url_resolve_from_to">url.resolve(from, to)</a></li>
</ul>
</li>
</ul>

          </div>

          <div id="apicontent">
            <h1>URL<span><a class="mark" href="#url_url" id="url_url">#</a></span></h1>
<pre class="api_stability_3">Stability: 3 - Stable</pre><p>This module has utilities for URL resolution and parsing.
Call <code>require(&#39;url&#39;)</code> to use it.

</p>
<p>Parsed URL objects have some or all of the following fields, depending on
whether or not they exist in the URL string. Any parts that are not in the URL
string will not be in the parsed object. Examples are shown for the URL

</p>
<p><code>&#39;http://user:pass@host.com:8080/p/a/t/h?query=string#hash&#39;</code>

</p>
<ul>
<li><p><code>href</code>: The full URL that was originally parsed. Both the protocol and host are lowercased.</p>
<p>  Example: <code>&#39;http://user:pass@host.com:8080/p/a/t/h?query=string#hash&#39;</code></p>
</li>
<li><p><code>protocol</code>: The request protocol, lowercased.</p>
<p>  Example: <code>&#39;http:&#39;</code></p>
</li>
<li><p><code>slashes</code>: The protocol requires slashes after the colon</p>
<p>  Example: true or false</p>
</li>
<li><p><code>host</code>: The full lowercased host portion of the URL, including port
information.</p>
<p>  Example: <code>&#39;host.com:8080&#39;</code></p>
</li>
<li><p><code>auth</code>: The authentication information portion of a URL.</p>
<p>  Example: <code>&#39;user:pass&#39;</code></p>
</li>
<li><p><code>hostname</code>: Just the lowercased hostname portion of the host.</p>
<p>  Example: <code>&#39;host.com&#39;</code></p>
</li>
<li><p><code>port</code>: The port number portion of the host.</p>
<p>  Example: <code>&#39;8080&#39;</code></p>
</li>
<li><p><code>pathname</code>: The path section of the URL, that comes after the host and
before the query, including the initial slash if present.</p>
<p>  Example: <code>&#39;/p/a/t/h&#39;</code></p>
</li>
<li><p><code>search</code>: The &#39;query string&#39; portion of the URL, including the leading
question mark.</p>
<p>  Example: <code>&#39;?query=string&#39;</code></p>
</li>
<li><p><code>path</code>: Concatenation of <code>pathname</code> and <code>search</code>.</p>
<p>  Example: <code>&#39;/p/a/t/h?query=string&#39;</code></p>
</li>
<li><p><code>query</code>: Either the &#39;params&#39; portion of the query string, or a
querystring-parsed object.</p>
<p>  Example: <code>&#39;query=string&#39;</code> or <code>{&#39;query&#39;:&#39;string&#39;}</code></p>
</li>
<li><p><code>hash</code>: The &#39;fragment&#39; portion of the URL including the pound-sign.</p>
<p>  Example: <code>&#39;#hash&#39;</code></p>
</li>
</ul>
<p>The following methods are provided by the URL module:

</p>
<h2>url.parse(urlStr, [parseQueryString], [slashesDenoteHost])<span><a class="mark" href="#url_url_parse_urlstr_parsequerystring_slashesdenotehost" id="url_url_parse_urlstr_parsequerystring_slashesdenotehost">#</a></span></h2>
<p>Take a URL string, and return an object.

</p>
<p>Pass <code>true</code> as the second argument to also parse
the query string using the <code>querystring</code> module.
Defaults to <code>false</code>.

</p>
<p>Pass <code>true</code> as the third argument to treat <code>//foo/bar</code> as
<code>{ host: &#39;foo&#39;, pathname: &#39;/bar&#39; }</code> rather than
<code>{ pathname: &#39;//foo/bar&#39; }</code>. Defaults to <code>false</code>.

</p>
<h2>url.format(urlObj)<span><a class="mark" href="#url_url_format_urlobj" id="url_url_format_urlobj">#</a></span></h2>
<p>Take a parsed URL object, and return a formatted URL string.

</p>
<p>Here&#39;s how the formatting process works:

</p>
<ul>
<li><code>href</code> will be ignored.</li>
<li><code>protocol</code> is treated the same with or without the trailing <code>:</code> (colon).<ul>
<li>The protocols <code>http</code>, <code>https</code>, <code>ftp</code>, <code>gopher</code>, <code>file</code> will be
postfixed with <code>://</code> (colon-slash-slash).</li>
<li>All other protocols <code>mailto</code>, <code>xmpp</code>, <code>aim</code>, <code>sftp</code>, <code>foo</code>, etc will
be postfixed with <code>:</code> (colon)</li>
</ul>
</li>
<li><code>slashes</code> set to <code>true</code> if the protocol requires <code>://</code> (colon-slash-slash)<ul>
<li>Only needs to be set for protocols not previously listed as requiring
slashes, such as <code>mongodb://localhost:8000/</code></li>
</ul>
</li>
<li><code>auth</code> will be used if present.</li>
<li><code>hostname</code> will only be used if <code>host</code> is absent.</li>
<li><code>port</code> will only be used if <code>host</code> is absent.</li>
<li><code>host</code> will be used in place of <code>hostname</code> and <code>port</code></li>
<li><code>pathname</code> is treated the same with or without the leading <code>/</code> (slash)</li>
<li><code>query</code> (object; see <code>querystring</code>) will only be used if <code>search</code> is absent.</li>
<li><code>search</code> will be used in place of <code>query</code>.<ul>
<li>It is treated the same with or without the leading <code>?</code> (question mark)</li>
</ul>
</li>
<li><code>hash</code> is treated the same with or without the leading <code>#</code> (pound sign, anchor)</li>
</ul>
<h2>url.resolve(from, to)<span><a class="mark" href="#url_url_resolve_from_to" id="url_url_resolve_from_to">#</a></span></h2>
<p>Take a base URL, and a href URL, and resolve them as a browser would for
an anchor tag.  Examples:

</p>
<pre><code>url.resolve(&#39;/one/two/three&#39;, &#39;four&#39;)         // &#39;/one/two/four&#39;
url.resolve(&#39;http://example.com/&#39;, &#39;/one&#39;)    // &#39;http://example.com/one&#39;
url.resolve(&#39;http://example.com/one&#39;, &#39;/two&#39;) // &#39;http://example.com/two&#39;</code></pre>

          </div>
        </div>
    </div>
    </div>
    <div id="footer">
      <div class="foot-1">
        <a href="http://www.joyent.com"><h5>The Node.js Project is Sponsored by</h5>
        <img src="assets/joyent-footer.svg" width="200">
        <p class="tag">Production Node +<br>High Performance Infrastructure</p></a>
        <a href="https://my.joyent.com/landing/signup/701800000015696" class="button getstarted">Get Started</a>
      </div>
      <div class="foot-2">
        <div class="foot-nav">
          <ul>
            <li><a href="http://nodejs.org/download/">Downloads</a></li>
          </ul>
          <ul>
            <li><a href="http://nodejs.org/documentation/">Documentation</a></li>
            <li><a href="http://nodejs.org/api/">API Docs</a></li>
            <li><a href="http://nodejs.org/documentation/tutorials/">Tutorials</a></li>
            <li><a href="http://nodejs.org/documentation/localization/">Localization</a></li>
          </ul>
          <ul>
            <li><a href="http://nodejs.org/community/">Community</a></li>
            <li><a href="https://github.com/joyent/node/issues">Github Issues</a></li>
            <li><a href="http://groups.google.com/group/nodejs">Mailing List</a></li>
            <li><a href="http://webchat.freenode.net/?channels=node.js">IRC</a></li>
            <li><a href="https://twitter.com/nodejs">Twitter</a></li>
          </ul>
          <ul>
            <li><a href="http://nodejs.org/about/">About</a></li>
            <li><a href="http://nodejs.org/about/organization/">Organization</a></li>
            <li><a href="http://nodejs.org/about/core-team/">Core Team</a></li>
            <li><a href="http://nodejs.org/about/resources/">Resources</a></li>
          </ul>
          <ul>
            <li><a href="http://blog.nodejs.org">Blog</a></li>
          </ul>
        </div>
        <p class="copyright">Copyright 2014 <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="https://nodejs.org/images/trademark-policy.pdf">trademark</a> of Joyent, Inc. <a href="https://raw.github.com/joyent/node/v0.10.35/LICENSE">View license</a>.</p>
      </div>
    </div>

  <script src="/sh_main.js"></script>
  <script src="/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
  <script>
    window._gaq = [['_setAccount', 'UA-10874194-2'], ['_trackPageview']];
    (function(d, t) {
      var g = d.createElement(t),
          s = d.getElementsByTagName(t)[0];
      g.src = '//www.google-analytics.com/ga.js';
      s.parentNode.insertBefore(g, s);
    }(document, 'script'));
  </script>
</body>
</html>

